package xijia.plus.docs.manage.all.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.github.wslxm.springbootplus2.core.base.model.BasePage;
import io.github.wslxm.springbootplus2.core.base.service.impl.BaseServiceImpl;
import io.github.wslxm.springbootplus2.core.enums.Base;
import io.github.wslxm.springbootplus2.core.utils.BeanDtoVoUtil;
import io.github.wslxm.springbootplus2.core.utils.validated.ValidUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import xijia.plus.docs.manage.all.mapper.DocCategoryMapper;
import xijia.plus.docs.manage.all.model.dto.DocCategoryDTO;
import xijia.plus.docs.manage.all.model.entity.DocCategory;
import xijia.plus.docs.manage.all.model.query.DocCategoryQuery;
import xijia.plus.docs.manage.all.model.vo.DocCategoryVO;
import xijia.plus.docs.manage.all.service.DocCategoryService;

import java.util.List;

/**
 * 文档类别 ServiceImpl
 *
 * <p>
 * ::本代码由[兮家小二]提供的代码生成器生成,如有问题,请手动修改 ::作者CSDN:https://blog.csdn.net/qq_41463655
 * </p>
 * @author ws
 * @email 1720696548@qq.com
 * @date 2023-04-23 11:03:15
 */
@Service
//@DS("#header.tenantName")
public class DocCategoryServiceImpl extends BaseServiceImpl<DocCategoryMapper, DocCategory> implements DocCategoryService {

    @Override
    @DS("#tenantName")
    public BasePage<DocCategoryVO> findPage(String tenantName,DocCategoryQuery query) {
        LambdaQueryWrapper<DocCategory> queryWrapper = new LambdaQueryWrapper<DocCategory>()
                .orderByAsc(DocCategory::getSort)
                .orderByDesc(DocCategory::getCreateTime);
        queryWrapper.eq(query.getCode() != null, DocCategory::getCode, query.getCode());
        queryWrapper.likeRight(StringUtils.isNotBlank(query.getName()), DocCategory::getName, query.getName());
        Page<DocCategory> page = this.page(new Page<>(query.getCurrent(), query.getSize()), queryWrapper);
        return BeanDtoVoUtil.pageVo(page, DocCategoryVO.class);
    }

    @Override
    @DS("#tenantName")
    public List<DocCategoryVO> list(String tenantName,DocCategoryQuery query) {
        LambdaQueryWrapper<DocCategory> queryWrapper = new LambdaQueryWrapper<DocCategory>()
                .orderByAsc(DocCategory::getSort)
                .orderByDesc(DocCategory::getCreateTime);
        queryWrapper.eq(DocCategory::getDisable, Base.Disable.V0.getValue());
        List<DocCategory> list = this.list(queryWrapper);
        return BeanDtoVoUtil.listVo(list, DocCategoryVO.class);
    }

    @Override
    @DS("#tenantName")
    public DocCategoryVO findId(String tenantName,String id) {
        return BeanDtoVoUtil.convert(this.getById(id), DocCategoryVO.class);
    }

    @Override
    @DS("#tenantName")
    public String insert(String tenantName,DocCategoryDTO dto) {
        DocCategory entity = dto.convert(DocCategory.class);
        this.verifyCodeRepeat(dto.getCode(), null);
        boolean b = this.save(entity);
        return entity.getId();
    }

    @Override
    @DS("#tenantName")
    public boolean upd(String tenantName,String id, DocCategoryDTO dto) {
        DocCategory entity = dto.convert(DocCategory.class);
        entity.setId(id);
        this.verifyCodeRepeat(dto.getCode(), id);
        return this.updateById(entity);
    }

    @Override
    @DS("#tenantName")
    public boolean del(String tenantName,String id) {
        return this.removeById(id);
    }

    @Override
    @DS("#tenantName")
    public String findCategoryName(String tenantName, Integer code) {
        DocCategory docCategory = this.getOne(new LambdaQueryWrapper<DocCategory>()
                .select(DocCategory::getName)
                .eq(DocCategory::getCode, code));
        return docCategory != null ? docCategory.getName() : null;
    }

    @Override
    @DS("#tenantName")
    public Integer findCategoryCode(String tenantName, String name) {
        DocCategory docCategory = this.getOne(new LambdaQueryWrapper<DocCategory>()
                .select(DocCategory::getCode)
                .eq(DocCategory::getName, name));
        return docCategory != null ? docCategory.getCode() : null;
    }


    /**
     * code重复验证
     *
     * @author wangsong
     * @mail 1720696548@qq.com
     * @date 2022/8/20 0020 14:33 验证
     * @version 1.0.0
     */
    private void verifyCodeRepeat(Integer code, String excludeId) {
        long count = this.count(new LambdaQueryWrapper<DocCategory>()
                .eq(DocCategory::getCode, code)
                .ne(excludeId != null, DocCategory::getId, excludeId)
        );
        ValidUtil.isTrue(count > 0,"code 重复");
    }
}
